﻿// Services/AmisService.cs
using AmisSdk.Builders;
using AmisSdk.Models.Components;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace AmisSdk.Services
{
    public class AmisService
    {
        /// <summary>
        /// 生成一个示例页面，包含一个表单。
        /// </summary>
        public string GenerateSamplePage()
        {
            var page = new Page
            {
                Title = "示例页面",
                Body = new Form
                {
                    Title = "用户表单",
                    Api = "/api/users",
                    Controls = new List<BaseComponent>
                    {
                        new TextInput
                        {
                            Name = "username",
                            Label = "用户名",
                            Placeholder = "请输入用户名",
                            Required = true,
                            Validation = new Validation
                            {
                                Required = true,
                                MinLength = 3,
                                MaxLength = 20,
                                Message = "用户名长度必须在3到20之间。"
                            }
                        },
                        new PasswordInput
                        {
                            Name = "password",
                            Label = "密码",
                            Placeholder = "请输入密码",
                            Required = true,
                            Validation = new Validation
                            {
                                Required = true,
                                MinLength = 6,
                                Message = "密码长度至少为6位。"
                            }
                        },
                        new SubmitButton
                        {
                            Label = "提交",
                            Level = "primary"
                        }
                    }
                }
            };

            var builder = new AmisBuilder()
                .SetBody(page);

            return builder.Build();
        }

        /// <summary>
        /// 生成一个包含数据表格的页面。
        /// </summary>
        public string GenerateUserTablePage()
        {
            var table = new Table
            {
                Title = "用户列表",
                Api = "/api/users",
                Columns = new List<Column>
                {
                    new Column { Name = "id", Label = "ID", Type = "text", Sortable = true, Width = "50px" },
                    new Column { Name = "username", Label = "用户名", Type = "text", Sortable = true },
                    new Column { Name = "email", Label = "邮箱", Type = "text" },
                    new Column { Name = "created_at", Label = "创建时间", Type = "date", Sortable = true, Width = "150px" }
                },
                Pagination = new Pagination
                {
                    PageSize = 20,
                    PageSizes = new List<int> { 10, 20, 50, 100 },
                    Position = "bottom"
                },
                Toolbar = new Toolbar
                {
                    Actions = new List<ActionButton>
                    {
                        new ActionButton
                        {
                            Label = "新增用户",
                            ActionType = "dialog",
                            Level = "primary",
                            Dialog = new Dialog
                            {
                                Title = "新增用户",
                                Body = new Form
                                {
                                    Title = "新增用户表单",
                                    Api = "/api/users/create",
                                    Controls = new List<BaseComponent>
                                    {
                                        new TextInput
                                        {
                                            Name = "username",
                                            Label = "用户名",
                                            Placeholder = "请输入用户名",
                                            Required = true
                                        },
                                        new PasswordInput
                                        {
                                            Name = "password",
                                            Label = "密码",
                                            Placeholder = "请输入密码",
                                            Required = true
                                        },
                                        new SubmitButton
                                        {
                                            Label = "提交",
                                            Level = "primary"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var page = new Page
            {
                Title = "用户管理",
                Body = table
            };

            var builder = new AmisBuilder()
                .SetBody(page);

            return builder.Build();
        }

        // 可以添加更多方法来生成不同类型的 AMIS 配置
    }
}
